<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mess extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->model('mess/mess_model');
	}
    function index()
    {
        $this->inbox();
    }
    
    function inbox()
    {
        if(!$this->session->userdata('username'))
        {
            redirect(site_url('login'));
            die();
        }
        $data = array();
        $uid = $this->session->userdata('uid');
        $info = $this->mess_model->ww_get_all_mail_by_user($uid, 0);
        $data['info'] = $info;
        // header
        
        // content
        $this->load->view('mess/mess_inbox_view', $data);
        
        // footer
    }
    
    function sent()
    {
        if(!$this->session->userdata('username'))
        {
            redirect(site_url('login'));
            die();
        }
        $data = array();
        $uid = $this->session->userdata('uid');
        $info = $this->mess_model->ww_get_all_mail_by_user($uid, 1);
        $data['info'] = $info;
        // header
        
        // content
        $this->load->view('mess/mess_sent_view', $data);
        
        // footer
    }
    
    function read()
    {
        if(!$this->session->userdata('username'))
        {
            redirect(site_url('login'));
            die();
        }
        $data = array();
        $messid = (int)$this->input->get('id');
        if($messid==0)
        {
            redirect(site_url('mess'));
            die();
        }
        $uid = $this->session->userdata('uid');
        $uname = $this->session->userdata('username');
        $mess = $this->mess_model->ww_mess_get_all_info($messid, $uid);
        if($mess)
        {
            $typeMess = $mess['owner']==$uid ? true : false;  // true=gui, false=nhan;
            if($mess['viewed']==0 && $uid==$mess['target'])
            {
                $this->mess_model->ww_update_read_mess_by_user($uid, $mess['id']);
            }
            $data['type'] = $typeMess;
            $data['info'] = $mess;
            $data['info']['message'] = str_replace('[b]', '<b>',$data['info']['message']);
            $data['info']['message'] = str_replace('[/b]', '</b>',$data['info']['message']);
            $data['info']['message'] = str_replace('[r]', '<span class="text-red">',$data['info']['message']);
            $data['info']['message'] = str_replace('[/r]', '</span>',$data['info']['message']);
            $data['info']['message'] = str_replace('[name]', '<span class="text-name">',$data['info']['message']);
            $data['info']['message'] = str_replace('[/name]', '</span>',$data['info']['message']);
            $data['id'] = $messid;
            $data['uname'] = $uname;
        }
        else
        {
            redirect(site_url('mess'));
            die();
        }
        // header
        
        // content
        $this->load->view('mess/mess_read_view', $data);
        
        // footer
    }
    
    function answer()
    {
        if(!$this->session->userdata('username'))
        {
            redirect(site_url('login'));
            die();
        }
        $data = array();
        $error = array();
        $error['errorTarget'] = '';
        $uid = $this->session->userdata('uid');
        $uname = $this->session->userdata('username');
        if($_SERVER['REQUEST_METHOD'] == 'POST')
        {
            $topic = $this->input->post('topic');
            $target = $this->input->post('target');
            $content = $this->input->post('nd');
            
            $chk_tp = $topic != '' ? true : false;
            $chk_tar = $target != '' ? true : false;
            $chk_cont = $content != '' ? true : false;
            
            if($target != '')
            {
                $to = $this->mess_model->ww_user_info($this->mycommon->getSaveSqlStr($target));
                
                if($to['id'] != '')
                {
                    $target = $to['id'];
                }
                else
                {
                    $error['errorTarget'] = 'Tài khoản không tồn tại';
                }
            }
            else
            {
                $error['errorTarget'] = 'Bạn hãy điền tài khoản gửi đến';
            }
            
            $chk_tar = $error['errorTarget'] != '' ? false : true;
            
            if($chk_tp == true && $chk_tar == true && $chk_cont == true)
            {
                $owner = $this->session->userdata('uid');
                $topic = $this->mycommon->getSaveSqlStr($topic);
                $content = '[name]>>> '.$uname.'[/name] viết:&#10;'.$this->mycommon->getSaveSqlStr($content);
                $this->mess_model->ww_add_mess($owner, $target, $topic, $content);
                redirect(site_url('mess/sent'));
                die();
            }
            
        }
        $data['error'] = $error;
        $messid = (int)$this->input->get('id');
        if($messid==0)
        {
            redirect(site_url('mess'));
            die();
        }
        $mess = $this->mess_model->ww_mess_get_all_info($messid, $uid);
        if($mess)
        {
            $typeMess = $mess['owner']==$uid ? true : false;  // true=gui, false=nhan;
            $data['type'] = $typeMess;
            $data['info'] = $mess;
            $data['uname'] = $uname;
        }
        else
        {
            redirect(site_url('mess'));
            die();
        }
        // header
        
        // content
        $this->load->view('mess/answer_view', $data);
        
        // footer
    }
    
    function write()
    {
        if(!$this->session->userdata('username'))
        {
            redirect(site_url('login'));
            die();
        }
        $data = array();
        $error = array();
        $error['errorTarget'] = '';
        $uid = $this->session->userdata('uid');
        $uname = $this->session->userdata('username');
        if($_SERVER['REQUEST_METHOD'] == 'POST')
        {
            $topic = $this->input->post('topic');
            $target = $this->input->post('target');
            $content = $this->input->post('nd');
            
            $chk_tp = $topic != '' ? true : false;
            $chk_tar = $target != '' ? true : false;
            $chk_cont = $content != '' ? true : false;
            
            if($target != '')
            {
                $to = $this->mess_model->ww_user_info($this->mycommon->getSaveSqlStr($target));
                
                if($to['id'] != '' && $to['id'] != $uid)
                {
                    $target = $to['id'];
                }
                else if($to['id'] == $uid)
                {
                    $error['errorTarget'] = 'Bạn không thể tự gửi thư cho mình.';
                }
                else
                {
                    $error['errorTarget'] = 'Tài khoản không tồn tại.';
                }
            }
            else
            {
                $error['errorTarget'] = 'Bạn hãy điền tài khoản gửi đến';
            }
            
            $chk_tar = $error['errorTarget'] != '' ? false : true;
            
            if($chk_tp == true && $chk_tar == true && $chk_cont == true)
            {
                $owner = $this->session->userdata('uid');
                $topic = $this->mycommon->sanitize($topic);
                $content = '[name]>>> '.$uname.'[/name] viết:&#10;'.$this->mycommon->getSaveSqlStr($content);
                //var_dump($owner, $target, $topic, $content);die();
                $this->mess_model->ww_add_mess($owner, $target, $topic, $content);
                redirect(site_url('mess/sent'));
                die();
            }
            
        }
        $data['error'] = $error;
        // header
        
        // content
        $this->load->view('mess/mess_send_view', $data);
        
        // footer
    }
 }